package com.onlineClass.edu.controller;

import cn.hutool.core.bean.BeanUtil;
import com.onlineClass.edu.base.BaseApiController;
import com.onlineClass.edu.base.BaseResponse;
import com.onlineClass.edu.entity.EduUser;
import com.onlineClass.edu.entity.dto.response.EduUserRespDto;
import com.onlineClass.edu.service.IEduUserService;
import com.onlineClass.edu.utils.RedisUtils;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 邓辉
 * @ClassName UserController
 * @date 2024-05-02 17:01
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController extends BaseApiController {
    @Autowired
    private IEduUserService iEduUserService;

    /**
     * 根据token获取用户身份信息
     * @param token
     * @return
     */
    @GetMapping
    public BaseResponse getUserInfoByToken(@RequestHeader String token){
        if (StringUtils.isEmpty(token)){
            log.error("[token无效]");
            return setResultError("[token无效]");
        }
        String redisUserID = RedisUtils.getString(token);
        if (StringUtils.isEmpty(redisUserID)){
            log.error("[userId不存在]");
            return setResultError("[userId不存在]");
        }
        int userID = Integer.parseInt(redisUserID);
        EduUser eduUser = iEduUserService.fingByUserID(userID);
        //do转dto进行数据脱敏
        EduUserRespDto eduUserRespDto = BeanUtil.copyProperties(eduUser, EduUserRespDto.class);
        return setResultSuccessData(eduUserRespDto);
    }
}
